<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="姓名" prop="memberName">
        <el-input v-model="dataForm.memberName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="卡号" prop="cardNo">
        <el-input v-model="dataForm.cardNo" placeholder="卡号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择性别">
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="当前积分" prop="currentPoint">
      <el-input v-model="dataForm.currentPoint" placeholder="当前积分"></el-input>
    </el-form-item>
    <el-form-item label="累计消费" prop="monetary">
      <el-input v-model="dataForm.monetary" placeholder="累计消费金额"></el-input>
      </el-form-item>-->
      <el-form-item label="所属店铺" prop="storeId">
        <el-select v-model="dataForm.storeId" placeholder="请选择店铺" :disabled="dataForm.id">
          <el-option
            v-for="item in dataForm.storeList"
            :key="item.id"
            :label="item.storeName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="家庭住址" prop="familyAddress">
      <el-input v-model="dataForm.familyAddress" placeholder="家庭住址"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="Email" prop="email">
      <el-input v-model="dataForm.email" placeholder="Email"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="QQ" prop="qq">
      <el-input v-model="dataForm.qq" placeholder="QQ"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="会员等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="会员等级" :disabled="true"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="折扣比例" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣比例"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="状态（0 无效；1 有效）" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态（0 无效；1 有效）"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="创建人" prop="author">
      <el-input v-model="dataForm.author" placeholder="创建人"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="修改人" prop="modifier">
      <el-input v-model="dataForm.modifier" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        cardNo: "",
        mobile: "",
        memberName: "",
        sex: "",
        currentPoint: "",
        monetary: "",
        storeId: "",
        familyAddress: "",
        email: "",
        qq: "",
        level: "",
        discount: "",
        state: "",
        storeLIst: []
      },
      dataRule: {
        cardNo: [{ required: true, message: "卡号不能为空", trigger: "blur" }],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        memberName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        // currentPoint: [
        //   { required: true, message: '当前积分不能为空', trigger: 'blur' }
        // ],
        // monetary: [
        //   { required: true, message: '累计消费金额不能为空', trigger: 'blur' }
        // ],
        storeId: [{ required: true, message: "店铺不能为空", trigger: "blur" }]
        // familyAddress: [
        //   { required: true, message: '家庭住址不能为空', trigger: 'blur' }
        // ],
        // email: [
        //   { required: true, message: 'Email不能为空', trigger: 'blur' }
        // ],
        // qq: [
        //   { required: true, message: 'QQ不能为空', trigger: 'blur' }
        // ],
        // level: [
        //   { required: true, message: '会员等级不能为空', trigger: 'blur' }
        // ],
        // discount: [
        //   { required: true, message: '折扣比例不能为空', trigger: 'blur' }
        // ],
        // state: [
        //   { required: true, message: '状态（0 无效；1 有效）不能为空', trigger: 'blur' }
        // ],
        // author: [
        //   { required: true, message: '创建人不能为空', trigger: 'blur' }
        // ],
        // createTime: [
        //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
        // ],
        // modifier: [
        //   { required: true, message: '修改人不能为空', trigger: 'blur' }
        // ],
        // modifyTime: [
        //   { required: true, message: '修改时间不能为空', trigger: 'blur' }
        // ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/bus/member/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.cardNo = data.member.cardNo;
              this.dataForm.mobile = data.member.mobile;
              this.dataForm.memberName = data.member.memberName;
              this.dataForm.sex = data.member.sex;
              this.dataForm.currentPoint = data.member.currentPoint;
              this.dataForm.monetary = data.member.monetary;
              this.dataForm.storeId = data.member.storeId;
              this.dataForm.familyAddress = data.member.familyAddress;
              this.dataForm.email = data.member.email;
              this.dataForm.qq = data.member.qq;
              this.dataForm.level = data.member.level;
              this.dataForm.discount = data.member.discount;
              this.dataForm.state = data.member.state;
              this.dataForm.author = data.member.author;
              this.dataForm.createTime = data.member.createTime;
              this.dataForm.modifier = data.member.modifier;
              this.dataForm.modifyTime = data.member.modifyTime;
            }
          });
        }
        //获取店铺列表
        this.$http({
          url: this.$http.adornUrl(`/bus/store/listByCondition`),
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataForm.storeList = data.list;
          }
        });
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/bus/member/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              cardNo: this.dataForm.cardNo,
              mobile: this.dataForm.mobile,
              memberName: this.dataForm.memberName,
              sex: this.dataForm.sex,
              currentPoint: this.dataForm.currentPoint,
              monetary: this.dataForm.monetary,
              storeId: this.dataForm.storeId,
              familyAddress: this.dataForm.familyAddress,
              email: this.dataForm.email,
              qq: this.dataForm.qq,
              level: this.dataForm.level,
              discount: this.dataForm.discount,
              state: this.dataForm.state,
              author: this.dataForm.author,
              createTime: this.dataForm.createTime,
              modifier: this.dataForm.modifier,
              modifyTime: this.dataForm.modifyTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
